//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.item;

import java.math.BigDecimal;
import net.sf.jasperreports.engine.*;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.item:
//            Item
public class ItemPrintDataSource
        implements JRDataSource {

    public ItemPrintDataSource(Item item, Object batchList[][], Parameter param) {
        this.item = item;
        this.batchList = batchList;
        this.param = param;
        totalQuantity = new BigDecimal("0.00");
        countPiecesListLine = 0;
        count = 34 * batchList.length;
    }

    public boolean next()
            throws JRException {
        return count > 0;
    }

    public Object getFieldValue(JRField field)
            throws JRException {
        String fieldname = field.getName();
        String value = null;
        if ("item_id".equals(fieldname)) {
            if (item.getId() != null) {
                value = "Code : " + item.getId().toString();
            }
        } else if ("last_modify".equals(fieldname)) {
            String date = item.getUpdateLastTime().substring(6, 8) + "/" + item.getUpdateLastTime().substring(4, 6) + "/" + item.getUpdateLastTime().substring(0, 4) + " " + item.getUpdateLastTime().substring(8, 10) + ":" + item.getUpdateLastTime().substring(10, 12) + ":" + item.getUpdateLastTime().substring(12);
            value = date;
        } else if ("designation".equals(fieldname)) {
            if (item.getDesignation() != null) {
                value = "D\351signation : " + item.getDesignation();
            }
        } else if ("gtin".equals(fieldname)) {
            if (item.getGtin() != null) {
                value = "GTIN : " + item.getGtin();
            }
        } else if ("item_family".equals(fieldname)) {
            if (item.getFamillesArticlesId() != null) {
                value = "Famille : " + item.getFamillesArticlesId();
            }
        } else if ("stock_place".equals(fieldname)) {
            if (item.getEmplacement() != null) {
                value = "Emplacement : " + item.getEmplacement();
            }
        } else if ("web".equals(fieldname)) {
            if (item.getWeb() != null) {
                value = "Visible sur le web : " + item.getWeb();
            }
        } else if ("description".equals(fieldname)) {
            if (item.getDescription() != null) {
                value = item.getDescription();
            }
        } else if ("notes".equals(fieldname)) {
            if (item.getCommentaire() != null) {
                value = item.getCommentaire();
            }
        } else if ("sales_unit".equals(fieldname)) {
            if (item.getUniteVente() != null) {
                value = "Unit\351 de vente : " + item.getUniteVente();
            }
        } else if ("packaging".equals(fieldname)) {
            if (item.getPackaging() != null) {
                value = "Conditionnement : " + item.getPackaging();
            }
        } else if ("alert_stock".equals(fieldname)) {
            if (item.getAlerte() != null) {
                value = "Stock d'alerte : " + item.getAlerte().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
            }
        } else if ("price_list2".equals(fieldname)) {
            if (item.getPriceList2() != null) {
                value = "Prix d'achat : " + item.getPriceList2().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
            }
        } else if ("supplier_id".equals(fieldname)) {
            if (item.getFournisseurId() != null) {
                value = "Fournisseur : " + item.getFournisseurId();
            }
        } else if ("tax".equals(fieldname)) {
            if (item.getTvaId() != null) {
                value = "TVA : " + item.getTvaId();
            }
        } else if ("balance_price".equals(fieldname)) {
            if (item.getPrixAchatPondere() != null) {
                value = "Prix pond\351rer : " + item.getPrixAchatPondere().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
            }
        } else if ("cost_price".equals(fieldname)) {
            if (item.getPrixRevient() != null) {
                value = "Prix de revient : " + item.getPrixRevient().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
            }
        } else if ("cost_price_detail".equals(fieldname)) {
            if (item.getDetailRevient() != null) {
                value = "D\351tails : " + item.getDetailRevient();
            }
        } else if ("price_list1".equals(fieldname)) {
            if (item.getPriceList1() != null) {
                value = "Prix de vente (TG 1) : " + item.getPriceList1().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
            }
        } else if ("price_list3".equals(fieldname)) {
            if (item.getPriceList3() != null) {
                value = "TG 2 (web) : " + item.getPriceList3().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
            }
        } else if ("price_list4".equals(fieldname)) {
            if (item.getPriceList4() != null) {
                value = "TG 3 (web) : " + item.getPriceList4().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
            }
        } else if ("promotion".equals(fieldname)) {
            if (item.getPromotion() != null) {
                value = "Promotion : " + item.getPromotion().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
            }
        } else if ("date".equals(fieldname)) {
            if (item.getDate() != null) {
                value = "Date : " + item.getDate();
            }
        } else if ("batch_txt".equals(fieldname)) {
            value = "N\260 de lot";
        } else if ("batch".equals(fieldname)) {
            value = batchList(0);
        } else if ("supplier_txt".equals(fieldname)) {
            value = "Code fournisseur";
        } else if ("supplier".equals(fieldname)) {
            value = batchList(1);
        } else if ("batch_date_txt".equals(fieldname)) {
            value = "Date";
        } else if ("batch_date".equals(fieldname)) {
            value = batchList(2);
        } else if ("quantity_txt".equals(fieldname)) {
            value = "Quantit\351";
        } else if ("quantity".equals(fieldname)) {
            value = batchList(3);
        } else if ("unit_price_txt".equals(fieldname)) {
            value = "Pu";
        } else if ("unit_price".equals(fieldname)) {
            value = batchList(4);
        } else if ("total_quantity".equals(fieldname)) {
            value = "Quantit\351 totale : " + totalQuantity.setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
        }
        count--;
        return value;
    }

    private String batchList(int col) {
        if (countPiecesListLine < batchList.length) {
            switch (col) {
                case 0: // '\0'
                    return (String) batchList[countPiecesListLine][col];

                case 1: // '\001'
                    return (String) batchList[countPiecesListLine][col];

                case 2: // '\002'
                    return (String) batchList[countPiecesListLine][col];

                case 3: // '\003'
                    if (batchList[countPiecesListLine][col] != null) {
                        totalQuantity = totalQuantity.add((BigDecimal) batchList[countPiecesListLine][col]);
                        return ((BigDecimal) batchList[countPiecesListLine][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 4: // '\004'
                    if (batchList[countPiecesListLine][col] != null) {
                        return ((BigDecimal) batchList[countPiecesListLine++][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }
            }
            return null;
        } else {
            return null;
        }
    }
    private Item item;
    private Object batchList[][];
    private Parameter param;
    private BigDecimal totalQuantity;
    private int count;
    private int countPiecesListLine;
}
